package com.commerceerp.system.controller;


import com.alibaba.fastjson.JSONArray;
import com.commerceerp.system.entity.*;
import com.commerceerp.system.service.FactoryService;
import com.commerceerp.system.service.ForderService;
import com.commerceerp.system.service.PurchaseOrderService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.RdPage;
import com.commerceerp.system.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(description = "采购接口", value = "采购接口")
@Scope("prototype")
@RestController
@EnableAutoConfiguration
public class PurchaseController {

    @Autowired
    private PurchaseOrderService porderService;

    @Autowired
    private ForderService forderService;

    @Autowired
    private FactoryService factoryService;



    @ApiOperation(value = "查询采购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderStorehouseId", value = "仓库id(全部不传)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "goodSystemChname", value = "商品中文名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "采购单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderStatus", value = "采购订单状态(1:建议中,2:计划中,3:待审核,4:待到货,5:已完成,6:已取消)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/getAllPlanPurchase", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllPlanPurchase( @RequestParam(value = "porderStorehouseId", required = false) String porderStorehouseId,
                                         @RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                         @RequestParam(value = "goodSystemChname", required = false) String goodSystemChname,
                                         @RequestParam(value = "porderStatus", required = false) String porderStatus,
                                         @RequestParam(value = "porderId", required = false) String porderId,
                                         @RequestParam(value = "current") Integer current,
                                         @RequestParam(value = "pageSize") Integer pageSize) throws IOException {

        ApiResult result = null;
        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));

        List<Map> list = porderService.getAllPlanPurchase(porderStorehouseId,goodSystemSku,goodSystemChname,porderStatus,porderId);

        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"查询成功",list,rdPage);

        return result;
    }

    @ApiOperation(value = "将建议采购加入计划采购")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "建议订单号", required = false, dataType = "String"),

    })
    @RequestMapping(value = "/updToPlanPorder", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updToPlanPorder( @RequestParam(value = "porderId", required = false) String porderId) throws IOException {

        ApiResult result = null;

        PorderInfo porderInfo = new PorderInfo();
        porderInfo.setPorderId(porderId);
        porderInfo.setPorderStatus("2");

        Integer success = porderService.updPorderInfoById(porderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }

    @ApiOperation(value = "修改计划中采购金额和数量信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "计划订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderPrice", value = "单价", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderAmount", value = "数量", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "porderTotalMoney", value = "总额", required = false, dataType = "Decimal"),

    })
    @RequestMapping(value = "/updPlanPorderPriceAndNum", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updPlanPorderPriceAndNum( @RequestParam(value = "porderId", required = false) String porderId,
                                      @RequestParam(value = "porderPrice", required = false) BigDecimal porderPrice,
                                      @RequestParam(value = "porderAmount", required = false) Integer porderAmount,
                                      @RequestParam(value = "porderTotalMoney", required = false) BigDecimal porderTotalMoney) throws IOException {

        ApiResult result = null;

        PorderInfo porderInfo = new PorderInfo();
        porderInfo.setPorderId(porderId);
        porderInfo.setPorderPrice(porderPrice);
        porderInfo.setPorderAmount(porderAmount);
        porderInfo.setPorderTotalMoney(porderTotalMoney);

        Integer success = porderService.updPorderInfoById(porderInfo);

        Map<String,Object> porder = this.porderService.getPorderInfoById(porderId);

        GoodInfo goodInfo = new GoodInfo();
        goodInfo.setGoodId(porder.get("porderGoodId").toString());
        goodInfo.setGoodPurchasePrice(porderPrice.doubleValue());

        Integer  updGoodSuccess = this.factoryService.updSystemGoodInfoById(goodInfo);

        if(success == 1 && updGoodSuccess ==1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "删除采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/delPorderInfoById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delPorderInfoById( @RequestParam(value = "porderId", required = false) String porderId) throws IOException {

        ApiResult result = null;

        Integer success = porderService.delPorderInfoById(porderId);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }



    @ApiOperation(value = "采购计划添加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderGoodId", value = "商品id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderStorehouseId", value = "存放仓库id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderSupplierId", value = "供应商", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderAmount", value = "数量", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "porderPrice", value = "单价", required = false, dataType = "Decimal"),
    })
    @RequestMapping(value = "/addGoodToPlanPorder", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoodToPlanPorder(@RequestBody String object) throws IOException {
        ApiResult result = null;

        List<Map<String,Object>> dataList = (List<Map<String,Object>>)JSONArray.parse(object);

        for(Map<String,Object> map : dataList){

            String porderGoodId = String.valueOf(map.get("porderGoodId"));
            BigDecimal porderPrice = BigDecimal.valueOf(Double.valueOf(map.get("porderPrice").toString()));
            Integer porderAmount = Integer.valueOf(map.get("porderAmount").toString());
            String porderSupplierId = String.valueOf(map.get("porderSupplierId"));
            BigDecimal porderTotalMoney = porderPrice.multiply(new BigDecimal(porderAmount));
            String porderStorehouseId = String.valueOf(map.get("porderStorehouseId"));
            String porderStatus = "2";

            PorderInfo porderInfo = new PorderInfo();
            porderInfo.setPorderId(StringUtils.getUUID());
            porderInfo.setPorderGoodId(porderGoodId);
            porderInfo.setPorderPrice(porderPrice);
            porderInfo.setPorderAmount(porderAmount);
            porderInfo.setPorderSupplierId(porderSupplierId);
            porderInfo.setPorderTotalMoney(porderTotalMoney);
            porderInfo.setPorderStorehouseId(porderStorehouseId);
            porderInfo.setPorderStatus(porderStatus);

            porderService.saveNewPorderInfo(porderInfo);


            GoodInfo goodInfo = new GoodInfo();
            goodInfo.setGoodId(porderGoodId);
            goodInfo.setGoodPurchasePrice(porderPrice.doubleValue());

            Integer  updGoodSuccess = this.factoryService.updSystemGoodInfoById(goodInfo);
        }

            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);

        return result;
    }


    @ApiOperation(value = "创建采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderGoodId", value = "商品id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderPrice", value = "单价", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderAmount", value = "数量", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "porderTotalMoney", value = "总额", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderSupplierId", value = "供应商", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderFreight", value = "运费", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderOtherPay", value = "其它费用", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderRemark", value = "备注", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderEstimateTime", value = "预计到达时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderStorehouseId", value = "存放仓库id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderDiscountMoney", value = "折扣价", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderStatus", value = "采购单状态(1:建议中,2:计划中,3:待审核,4:待到货,5:已完成,6:已取消)", required = false, dataType = "Decimal"),
            
    })
    @RequestMapping(value = "/saveNewPorderInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveNewPorderInfo(@RequestBody String object) throws IOException {

        ApiResult result = null;

        List<Map<String,Object>> dataList = (List<Map<String,Object>>)JSONArray.parse(object);

        for(Map<String,Object> map : dataList){

            String porderGoodId = String.valueOf(map.get("porderGoodId"));
            BigDecimal porderPrice = BigDecimal.valueOf(Double.valueOf(map.get("porderPrice").toString()));
            Integer porderAmount = Integer.valueOf(map.get("porderAmount").toString());
            String porderSupplierId = String.valueOf(map.get("porderSupplierId"));
            BigDecimal porderFreight = null;
            if(map.get("porderFreight") !=null ){
                porderFreight = BigDecimal.valueOf(Double.valueOf(map.get("porderFreight").toString()));
            }
            BigDecimal porderOtherPay = null;
            if(map.get("porderOtherPay") !=null ){
                porderOtherPay = BigDecimal.valueOf(Double.valueOf(map.get("porderOtherPay").toString()));
            }
            String porderRemark = null;
            if(map.get("porderRemark") !=null ){
                porderRemark = String.valueOf(map.get("porderRemark"));
            }
            BigDecimal porderTotalMoney = BigDecimal.valueOf(Double.valueOf(map.get("porderTotalMoney").toString()));
            String porderStorehouseId = String.valueOf(map.get("porderStorehouseId"));
            BigDecimal porderDiscountMoney = null;
            if(map.get("porderDiscountMoney") !=null ){
                porderDiscountMoney = BigDecimal.valueOf(Double.valueOf(map.get("porderDiscountMoney").toString()));
            }
            String porderStatus = String.valueOf(map.get("porderStatus"));
            String porderEstimateTime = String.valueOf(map.get("porderEstimateTime"));

            PorderInfo porderInfo = new PorderInfo();
            porderInfo.setPorderId(StringUtils.getUUID());
            porderInfo.setPorderGoodId(porderGoodId);
            porderInfo.setPorderPrice(porderPrice);
            porderInfo.setPorderAmount(porderAmount);
            porderInfo.setPorderSupplierId(porderSupplierId);
            porderInfo.setPorderFreight(porderFreight);
            porderInfo.setPorderOtherPay(porderOtherPay);
            porderInfo.setPorderRemark(porderRemark);
            porderInfo.setPorderTotalMoney(porderTotalMoney);
            porderInfo.setPorderStorehouseId(porderStorehouseId);
            porderInfo.setPorderDiscountMoney(porderDiscountMoney);
            porderInfo.setPorderCreateTime(DateUtil.getYMDHMSDate());
            porderInfo.setPorderStatus(porderStatus);
            porderInfo.setPorderEstimateTime(porderEstimateTime);
            porderInfo.setPorderTotalMoney(porderTotalMoney);
            StringUtils.getPorderSerialNumber(porderInfo);
             porderService.saveNewPorderInfo(porderInfo);

            GoodInfo goodInfo = new GoodInfo();
            goodInfo.setGoodId(porderGoodId);
            goodInfo.setGoodPurchasePrice(porderPrice.doubleValue());

            Integer  updGoodSuccess = this.factoryService.updSystemGoodInfoById(goodInfo);
        }

            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);


        return result;
    }


    @ApiOperation(value = "提交采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "计划采购单id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderFreight", value = "运费", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderOtherPay", value = "其它费用", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderEstimateTime", value = "预计到达时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderRemark", value = "备注", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/submitPlanPorder", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult submitPlanPorder(@RequestBody String object) throws IOException {
        ApiResult result = null;

        List<Map<String,Object>> dataList = (List<Map<String,Object>>)JSONArray.parse(object);

        for(Map<String,Object> map : dataList){

            String porderId = String.valueOf(map.get("porderId"));
            String porderEstimateTime = String.valueOf(map.get("porderEstimateTime"));
            String porderRemark = String.valueOf(map.get("porderRemark"));
            BigDecimal porderFreight = BigDecimal.valueOf(Double.valueOf(map.get("porderFreight").toString()));
            BigDecimal porderOtherPay = BigDecimal.valueOf(Double.valueOf(map.get("porderOtherPay").toString()));
            String porderCreateTime = DateUtil.getYMDHMSDate();
            String porderStatus = "3";

            PorderInfo porderInfo = new PorderInfo();
            porderInfo.setPorderId(porderId);
            porderInfo.setPorderEstimateTime(porderEstimateTime);
            porderInfo.setPorderRemark(porderRemark);
            porderInfo.setPorderFreight(porderFreight);
            porderInfo.setPorderOtherPay(porderOtherPay);
            porderInfo.setPorderCreateTime(porderCreateTime);
            porderInfo.setPorderStatus(porderStatus);
            StringUtils.getPorderSerialNumber(porderInfo);
            porderService.updPorderInfoById(porderInfo);
        }

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);

        return result;
    }


    @ApiOperation(value = "审核/取消采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "(1:审核通过，2:取消)", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/examineOrCancelPorderInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult examineOrCancelPorderInfo( @RequestParam(value = "porderId", required = false) String porderId,
                                                @RequestParam(value = "type", required = false) String type) throws IOException {

        ApiResult result = null;

        PorderInfo porderInfo = new PorderInfo();
        porderInfo.setPorderId(porderId);
        if("1".equals(type)){
            porderInfo.setPorderStatus("4");
        } else if("2".equals(type)){
            porderInfo.setPorderStatus("6");
        }
        Integer success = porderService.updPorderInfoById(porderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "添加运单号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderWaybillNumber", value = "运单号", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/addWaybillNumber", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addWaybillNumber( @RequestParam(value = "porderId", required = false) String porderId,
                                                @RequestParam(value = "porderWaybillNumber", required = false) String porderWaybillNumber) throws IOException {

        ApiResult result = null;

        PorderInfo porderInfo = new PorderInfo();
        porderInfo.setPorderId(porderId);
        porderInfo.setPorderWaybillNumber(porderWaybillNumber);
        Integer success = porderService.updPorderInfoById(porderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "编辑采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "采购单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderPrice", value = "单价", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderAmount", value = "数量", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "porderTotalMoney", value = "总额", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderSupplierId", value = "供应商", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderFreight", value = "运费", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderOtherPay", value = "其它费用", required = false, dataType = "Decimal"),
            @ApiImplicitParam(paramType = "query", name = "porderRemark", value = "备注", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderEstimateTime", value = "预计到达时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderStorehouseId", value = "存放仓库id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderDiscountMoney", value = "折扣价", required = false, dataType = "Decimal"),

    })
    @RequestMapping(value = "/updPorderInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updPorderInfo(@RequestParam(value = "porderId", required = false) String porderId,
                                       @RequestParam(value = "porderPrice", required = false)BigDecimal porderPrice,
                                       @RequestParam(value = "porderAmount", required = false) Integer porderAmount,
                                       @RequestParam(value = "porderTotalMoney", required = false) BigDecimal porderTotalMoney,
                                       @RequestParam(value = "porderSupplierId", required = false) String porderSupplierId,
                                       @RequestParam(value = "porderFreight", required = false) BigDecimal porderFreight,
                                       @RequestParam(value = "porderOtherPay", required = false) BigDecimal porderOtherPay,
                                       @RequestParam(value = "porderRemark", required = false) String porderRemark,
                                       @RequestParam(value = "porderEstimateTime", required = false) String porderEstimateTime,
                                       @RequestParam(value = "porderStorehouseId", required = false) String porderStorehouseId,
                                       @RequestParam(value = "porderDiscountMoney", required = false) BigDecimal porderDiscountMoney) throws IOException {

        ApiResult result = null;


            PorderInfo porderInfo = new PorderInfo();
            porderInfo.setPorderId(porderId);
            porderInfo.setPorderPrice(porderPrice);
            porderInfo.setPorderAmount(porderAmount);
            porderInfo.setPorderSupplierId(porderSupplierId);
            porderInfo.setPorderFreight(porderFreight);
            porderInfo.setPorderOtherPay(porderOtherPay);
            porderInfo.setPorderRemark(porderRemark);
            porderInfo.setPorderTotalMoney(porderTotalMoney);
            porderInfo.setPorderStorehouseId(porderStorehouseId);
            porderInfo.setPorderDiscountMoney(porderDiscountMoney);
            porderInfo.setPorderEstimateTime(porderEstimateTime);
            porderInfo.setPorderTotalMoney(porderTotalMoney);

        Integer success = porderService.updPorderInfoById(porderInfo);


        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "设置到货")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "porderArriveNumber", value = "到货量", required = false, dataType = "Integer"),
    })
    @RequestMapping(value = "/updArriveNumber", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updArriveNumber( @RequestParam(value = "porderId", required = false) String porderId,
                                       @RequestParam(value = "porderArriveNumber", required = false) Integer porderArriveNumber) throws IOException {

        ApiResult result = null;
        PorderInfo porderInfo = new PorderInfo();


        Map<String,Object> porder = porderService.getPorderInfoById(porderId);
        Integer oldPorderArriveNumber = Integer.parseInt(porder.get("porderArriveNumber").toString());
        Integer porderAmount = Integer.parseInt(porder.get("porderAmount").toString());
        Integer newPorderArriveNumber = oldPorderArriveNumber + porderArriveNumber;

        if(newPorderArriveNumber >= porderAmount){

            porderInfo.setPorderStatus("5");

        }

        porderInfo.setPorderId(porderId);
        porderInfo.setPorderArriveNumber(newPorderArriveNumber);
        Integer success = porderService.updPorderInfoById(porderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }

    @ApiOperation(value = "设置采购单已完成")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/updPorderToFinish", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updPorderToFinish( @RequestParam(value = "porderId", required = false) String porderId) throws IOException {

        ApiResult result = null;

        PorderInfo porderInfo = new PorderInfo();
        porderInfo.setPorderId(porderId);
        porderInfo.setPorderStatus("5");
        Integer success = porderService.updPorderInfoById(porderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }

    @ApiOperation(value = "还原已取消采购单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/returnCancelPorder", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult returnCancelPorder( @RequestParam(value = "porderId", required = false) String porderId) throws IOException {

        ApiResult result = null;

        PorderInfo porderInfo = new PorderInfo();
        porderInfo.setPorderId(porderId);
        porderInfo.setPorderStatus("4");
        Integer success = porderService.updPorderInfoById(porderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }

    @ApiOperation(value = "申请付款/申请收款")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "forderType", value = "财务单类型", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "1:付款，2:收款", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/applyPayOrReceivables", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult applyPayOrReceivables( @RequestParam(value = "porderId", required = false) String porderId,
                                            @RequestParam(value = "forderType", required = false) String forderType,
                                            @RequestParam(value = "type", required = false) String type) throws IOException {

        ApiResult result = null;

        Map<String,Object> porderInfo = porderService.getPorderInfoById(porderId);

        PorderInfo newPorderInfo = new PorderInfo();
        newPorderInfo.setPorderApplyPay("1");
        newPorderInfo.setPorderId(porderInfo.get("porderId").toString());

        Integer updPorderSuccess = this.porderService.updPorderInfoById(newPorderInfo);


        ForderInfo forderInfo = new ForderInfo();
        forderInfo.setForderId(StringUtils.getUUID());
        forderInfo.setForderStatus("1");
        forderInfo.setForderMoney(BigDecimal.valueOf(Double.valueOf(porderInfo.get("porderTotalMoney").toString())));
        forderInfo.setForderCreateTime(DateUtil.getYMDHMSDate());
        forderInfo.setForderType(forderType);
        forderInfo.setForderPorderId(porderId);
        if("1".equals(type)){
            forderInfo.setForderInOut("1");
        } else if("2".equals(type)){
            forderInfo.setForderInOut("2");
        }
        Integer success = forderService.saveNewForderInfo(forderInfo);

        if(success == 1 && updPorderSuccess==1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "采购单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "porderId", value = "订单号", required = false, dataType = "String"),

    })
    @RequestMapping(value = "/getPorderInfoById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getPorderInfoById( @RequestParam(value = "porderId", required = false) String porderId) throws IOException {

        ApiResult result = null;

        Map<String,Object> porderInfo = porderService.getPorderInfoById(porderId);

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",porderInfo,null);

        return result;
    }


    @ApiOperation(value = "采购页面各状态数量")
    @ApiImplicitParams({

    })
    @RequestMapping(value = "/getCountByPorderStatus", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getCountByPorderStatus( ) throws IOException {

        ApiResult result = null;

        List<Map> list = porderService.getCountByPorderStatus();

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",list,null);

        return result;
    }




}
